using System;
using System.Drawing;
using System.Windows.Forms;
using Spread.Common.Features.Sample;
using Spread.Common.DataStore;
using FarPoint.Win.Spread.Model;

namespace Spread.Worksheet.Features
{
    public partial class DragDropDemo : BaseSampleControl
    {
        private string s;
        private CellRange _range;
        CellRange _currentSelection;
        public DragDropDemo()
        {
            InitializeComponent();
            InitializeControls();
            SetupSpreadSheet();
        }
        private void InitializeControls()
        {
            Name = "Drag and Drop";
            spreadDestination.Sheets[0].Rows.Default.Height = 25;
            spreadSource.Sheets[0].Rows.Default.Height = 25;
            spreadDestination.AllowDrop = true;
        }
        private void SetupSpreadSheet()
        {
            spreadSource.DataSource = new DataService().AccessDataSource.GetNWINDData("Customers");
            sheetViewDestination.Columns.Count = sheetViewSource.Columns.Count;
            for(int _column = 0; _column < sheetViewSource.Columns.Count; _column++)
            {
                sheetViewDestination.Columns[_column].Width = sheetViewSource.Columns[_column].Width;
            }
        }

        private void SpreadDestination_DragOver(object sender, DragEventArgs e)
        {
            Point _point;
            CellRange _previousSelection;

            _point = spreadDestination.PointToClient(new Point(e.X, e.Y));
            _currentSelection = spreadDestination.GetCellFromPixel(0, 0, _point.X, _point.Y);
            if (_currentSelection.ColumnCount != -1 && _currentSelection.RowCount != -1)
            {
                _previousSelection = sheetViewDestination.GetSelection(0);

                if (!(_previousSelection == null))
                    sheetViewDestination.RemoveSelection(_previousSelection.Row, _previousSelection.Column, _previousSelection.RowCount, _previousSelection.ColumnCount);

                    sheetViewDestination.AddSelection(_currentSelection.Row, _currentSelection.Column, _range.RowCount, _range.ColumnCount);
            }
        }
        private void SpreadDestination_DragDrop(object sender, DragEventArgs e)
        {
            CellRange _selection;
            _selection = sheetViewDestination.GetSelection(0);
            if (_currentSelection != null)
            {
                sheetViewDestination.SetClip(_selection.Row, _selection.Column, _selection.RowCount, _selection.ColumnCount,s);
                sheetViewSource.RemoveSelection(_range.Row, _range.Column, _range.RowCount, _range.ColumnCount);
            }
        }
        private void SpreadDestination_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void SpreadSource_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && s!=null)
            {
                spreadSource.DoDragDrop(s,(DragDropEffects.Copy | DragDropEffects.Move));
            }
        }
        private void SpreadSource_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                _range = sheetViewSource.GetSelection(0);
                if (_range == null)
                    return;

                s = sheetViewSource.GetClip(_range.Row, _range.Column, _range.RowCount, _range.ColumnCount);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            indexTextBox.Text = string.Format("Row[{0}],Col[{1}]", _range.Row, _range.Column);
            countTextBox.Text = string.Format("RowCount[{0}],ColumnCount[{1}]", _range.RowCount, _range.ColumnCount);
           
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            spreadDestination.Reset();
            spreadDestination.AllowDrop = true;
            for (int _column = 0; _column < sheetViewSource.Columns.Count; _column++)
            {
                sheetViewDestination.Columns[_column].Width = sheetViewSource.Columns[_column].Width;
            }
        }

        public override Image GetImage(int width = 200, int height = 200)
        {
            spreadDestination.AllowDrop = false;
            return base.GetImage(width, height);
        }
    }
}


//
//DragDropDemo.Designer.cs
//


namespace Spread.Worksheet.Features
{
    partial class DragDropDemo
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.tablePanel = new System.Windows.Forms.TableLayoutPanel();
            this.spreadSource = new FarPoint.Win.Spread.FpSpread();
            this.sheetViewSource = new FarPoint.Win.Spread.SheetView();
            this.spreadDestination = new FarPoint.Win.Spread.FpSpread();
            this.sheetViewDestination = new FarPoint.Win.Spread.SheetView();
            this.selectionLabel = new System.Windows.Forms.Label();
            this.indexTextBox = new System.Windows.Forms.TextBox();
            this.countTextBox = new System.Windows.Forms.TextBox();
            this.resetButton = new System.Windows.Forms.Button();
            this.detailTextBox = new System.Windows.Forms.TextBox();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            this.tablePanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.spreadSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewSource)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.spreadDestination)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewDestination)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.tablePanel);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.detailTextBox);
            this.pnlControls.Controls.Add(this.resetButton);
            this.pnlControls.Controls.Add(this.countTextBox);
            this.pnlControls.Controls.Add(this.indexTextBox);
            this.pnlControls.Controls.Add(this.selectionLabel);
            // 
            // tablePanel
            // 
            this.tablePanel.ColumnCount = 1;
            this.tablePanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
            this.tablePanel.Controls.Add(this.spreadSource, 0, 0);
            this.tablePanel.Controls.Add(this.spreadDestination, 0, 1);
            this.tablePanel.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tablePanel.Location = new System.Drawing.Point(0, 0);
            this.tablePanel.Name = "tablePanel";
            this.tablePanel.RowCount = 2;
            this.tablePanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tablePanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
            this.tablePanel.Size = new System.Drawing.Size(477, 487);
            this.tablePanel.TabIndex = 0;
            // 
            // spreadSource
            // 
            this.spreadSource.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spreadSource.Location = new System.Drawing.Point(3, 3);
            this.spreadSource.Name = "spreadSource";
            this.spreadSource.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetViewSource});
            this.spreadSource.Size = new System.Drawing.Size(471, 237);
            this.spreadSource.TabIndex = 0;
            this.spreadSource.MouseDown += new System.Windows.Forms.MouseEventHandler(this.SpreadSource_MouseDown);
            this.spreadSource.MouseUp += new System.Windows.Forms.MouseEventHandler(this.SpreadSource_MouseUp);
            // 
            // sheetViewSource
            // 
            this.sheetViewSource.Reset();
            this.sheetViewSource.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetViewSource.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetViewSource.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // spreadDestination
            // 
            this.spreadDestination.Dock = System.Windows.Forms.DockStyle.Fill;
            this.spreadDestination.Location = new System.Drawing.Point(3, 246);
            this.spreadDestination.Name = "spreadDestination";
            this.spreadDestination.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.sheetViewDestination});
            this.spreadDestination.Size = new System.Drawing.Size(471, 238);
            this.spreadDestination.TabIndex = 1;
            this.spreadDestination.DragDrop += new System.Windows.Forms.DragEventHandler(this.SpreadDestination_DragDrop);
            this.spreadDestination.DragEnter += new System.Windows.Forms.DragEventHandler(this.SpreadDestination_DragEnter);
            this.spreadDestination.DragOver += new System.Windows.Forms.DragEventHandler(this.SpreadDestination_DragOver);
            // 
            // sheetViewDestination
            // 
            this.sheetViewDestination.Reset();
            this.sheetViewDestination.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.sheetViewDestination.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.sheetViewDestination.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // selectionLabel
            // 
            this.selectionLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.selectionLabel.AutoSize = true;
            this.selectionLabel.ForeColor = System.Drawing.Color.Blue;
            this.selectionLabel.Location = new System.Drawing.Point(2, 13);
            this.selectionLabel.Name = "selectionLabel";
            this.selectionLabel.Size = new System.Drawing.Size(96, 13);
            this.selectionLabel.TabIndex = 0;
            this.selectionLabel.Text = "You have selected";
            // 
            // indexTextBox
            // 
            this.indexTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.indexTextBox.BackColor = System.Drawing.SystemColors.Window;
            this.indexTextBox.Location = new System.Drawing.Point(5, 47);
            this.indexTextBox.Name = "indexTextBox";
            this.indexTextBox.ReadOnly = true;
            this.indexTextBox.Size = new System.Drawing.Size(130, 20);
            this.indexTextBox.TabIndex = 1;
            // 
            // countTextBox
            // 
            this.countTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.countTextBox.BackColor = System.Drawing.SystemColors.Window;
            this.countTextBox.Location = new System.Drawing.Point(5, 85);
            this.countTextBox.Name = "countTextBox";
            this.countTextBox.ReadOnly = true;
            this.countTextBox.Size = new System.Drawing.Size(130, 20);
            this.countTextBox.TabIndex = 2;
            // 
            // resetButton
            // 
            this.resetButton.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.resetButton.Location = new System.Drawing.Point(5, 122);
            this.resetButton.Name = "resetButton";
            this.resetButton.Size = new System.Drawing.Size(130, 23);
            this.resetButton.TabIndex = 3;
            this.resetButton.Text = "Reset";
            this.resetButton.UseVisualStyleBackColor = true;
            this.resetButton.Click += new System.EventHandler(this.resetButton_Click);
            // 
            // detailTextBox
            // 
            this.detailTextBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.detailTextBox.BackColor = System.Drawing.SystemColors.Window;
            this.detailTextBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.detailTextBox.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(192)))));
            this.detailTextBox.Location = new System.Drawing.Point(6, 152);
            this.detailTextBox.Multiline = true;
            this.detailTextBox.Name = "detailTextBox";
            this.detailTextBox.ReadOnly = true;
            this.detailTextBox.Size = new System.Drawing.Size(129, 319);
            this.detailTextBox.TabIndex = 4;
            this.detailTextBox.Text = "The row and columns are 0-based.  The selection above represents the starting row" +
    ", starting column, the number of rows selected and the number of columns selecte" +
    "d.";
            // 
            // DragDropDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "DragDropDemo";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            this.pnlControls.PerformLayout();
            this.tablePanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.spreadSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewSource)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.spreadDestination)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.sheetViewDestination)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion
        private System.Windows.Forms.TableLayoutPanel tablePanel;
        private FarPoint.Win.Spread.FpSpread spreadSource;
        private FarPoint.Win.Spread.SheetView sheetViewSource;
        private System.Windows.Forms.Button resetButton;
        private System.Windows.Forms.TextBox countTextBox;
        private System.Windows.Forms.TextBox indexTextBox;
        private System.Windows.Forms.Label selectionLabel;
        private FarPoint.Win.Spread.FpSpread spreadDestination;
        private FarPoint.Win.Spread.SheetView sheetViewDestination;
        private System.Windows.Forms.TextBox detailTextBox;
    }
}
